package aom.ju.ss.view.user.shop.management.freightTemplate

import android.content.Intent
import aom.ju.ss.R
import aom.ju.ss.databinding.ShippingTemplateSettingsActivityBinding
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.utils.show
import aom.ju.ss.view.base.BaseActivity
import aom.ju.ss.view.dialog.NationalFreeShippingMode
import aom.ju.ss.view.dialog.StorePickupMode
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName


/**
 * 运费模板设置
 */
class ShippingTemplateSettings : BaseActivity() {
    private lateinit var binding: ShippingTemplateSettingsActivityBinding
    override fun init() {
        binding = viewBinding as ShippingTemplateSettingsActivityBinding
        binding.selfMention.setOnClickListener {//自提
            StorePickupMode(activity)

        }

        binding.freeShipping.setOnClickListener {//包邮
            NationalFreeShippingMode(activity)
        }

        binding.noMail.setOnClickListener {//不包邮
            if (mode == null) {
                startActivityForResult(Intent(activity, EditShippingTemplate::class.java), 999)
            } else {
                startActivityForResult(
                    Intent(activity, EditShippingTemplate::class.java)
                        .putExtra("mode", Gson().toJson(mode)), 999
                )
            }

        }
        getData

    }

    val getData: Unit
        get() {
            OKHttpUtils(activity)
                .SetApiUrl(LP_API.get_freight_tmp_list)
                .POST(this)
        }

    private var mode: PostageTemplate? = null
    override fun ok(jsonString: String, httpTY: String) {
        super.ok(jsonString, httpTY)
        mode = Gson().fromJson(jsonString, PostageTemplate::class.java)
        mode?.data.let {
            if (it == null || it.isEmpty()) {
                binding.setType.text = "未设置"
            } else {
                binding.setType.text = "修改"
            }
        }
    }

    override fun error(e: String, jsonString: String, httpTY: String) {
        super.error(e, jsonString, httpTY)
        e?.show
    }

    override val lay: Int = R.layout.shipping_template_settings_activity

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 999 && resultCode == RESULT_OK) {
            getData
        }
    }
}

class PostageTemplate(
    @SerializedName("data")
    var `data`: List<PostageTemplateData>?,
    @SerializedName("mark")
    var mark: String,
    @SerializedName("mess")
    var mess: String,
    @SerializedName("other")
    var other: String,
    @SerializedName("status")
    var status: Int
)

data class PostageTemplateData(
    @SerializedName("addtime")
    var addtime: Int,
    @SerializedName("id")
    var id: Int,
    @SerializedName("province")
    var province: List<String>,
    @SerializedName("shipping_name")
    var shippingName: String,
    @SerializedName("shipping_state")
    var shippingState: Int,
    @SerializedName("shop_id")
    var shopId: Int,
    @SerializedName("type")
    var type: Int,
    @SerializedName("update_time")
    var updateTime: Int,
    @SerializedName("valuation_method")
    var valuationMethod: Int
)
